package com.itheima.controller;

///orderSetting/upload.do

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.utils.POIUtils;
import com.itheima.common.vo.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("orderSetting")
public class OrderSettingController {

    @Reference(version = "1.0")
    private OrderSettingService orderSettingService;

    @PostMapping("upload")
    public Result upload(MultipartFile excelFile){
        List<String[]> list = null;
        try {
            list = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : list) {
                //0 日期 1 可预约数量
                Date orderDate = new Date(strings[0]);
                Integer number = Integer.parseInt(strings[1]);
                //存数据库的操作，交给dubbo服务处理
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                orderSetting.setReservations(0);
                orderSettingList.add(orderSetting);
            }
            return orderSettingService.save(orderSettingList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.fail();
    }



    //"/orderSetting/findByMonth?month
    @GetMapping("findByMonth")
    public Result findByMonth(String month){
        return this.orderSettingService.findByMonth(month);
    }

    ///orderSetting/updateNumber?date
    @GetMapping("updateNumber")
    public Result updateNumber(String date,Integer number){
        return this.orderSettingService.updateNumber(date,number);
    }
}
